package bairui.international.weixin.portal.miniapp.controller.branchCompany.staff;

import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.querydsl.QSort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.querydsl.core.BooleanBuilder;

import bairui.international.model.domain.jpa.QTB_BranchCompanyStaffWithdrawOrder;
import bairui.international.model.domain.jpa.QTB_BranchCompanyWithdrawOrder;
import bairui.international.model.domain.jpa.TB_BranchCompanyStaff;
import bairui.international.model.domain.jpa.TB_BranchCompanyStaffWithdrawOrder;
import bairui.international.model.domain.jpa.TB_BranchCompanyWithdrawOrder;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.model.status.State_BranchCompanyStaffWithdrawOrder;
import bairui.international.model.status.State_BranchCompanyWithdrawOrder;
import bairui.international.service.Service_BranchCompanyStaff;
import bairui.international.service.Service_BranchCompanyStaffWithdrawOrder;
import bairui.international.service.Service_BranchCompanyWithdrawOrder;
import bairui.international.service.Service_User;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 *
 * 提现
 *
 * @generated
 * 
 * @see STest_C_BranchCompnay_Staff_Withdraw_List 测试
 *
 */
@Controller
public class C_BranchCompnay_Staff_Withdraw_List
 extends SC_BranchCompnay_Staff_Withdraw_List 
{
	
	@Autowired private Service_BranchCompanyWithdrawOrder service_BranchCompanyWithdrawOrder;
	@Autowired private Service_BranchCompanyStaffWithdrawOrder service_BranchCompanyStaffWithdrawOrder;
	@Autowired private Service_User service_User;
	@Autowired private Service_BranchCompanyStaff service_BranchCompanyStaff;
	private static final QTB_BranchCompanyWithdrawOrder QBranchCompanyWithdrawOrder = QTB_BranchCompanyWithdrawOrder.tB_BranchCompanyWithdrawOrder;
	private static final QTB_BranchCompanyStaffWithdrawOrder QBranchCompanyStaffWithdrawOrder = QTB_BranchCompanyStaffWithdrawOrder.tB_BranchCompanyStaffWithdrawOrder;
	
	/**
	 * 列表
	 */
	@Action(List.class)
	@GetMapping(List.RequestMappingValue)
	public @FomatOutput @ResponseBody Object list(
		ModelMap map, HttpServletRequest request,
		@RequestParam Integer startPage,
		@RequestParam String skey,
		@RequestParam(required=false, defaultValue="0") Integer state
	) throws Exception {
		TB_User user = service_User.findOne(it->it.skey.eq(skey));
		TB_BranchCompanyStaff branchCompanyStaff = service_BranchCompanyStaff.getActive(user);
		BooleanBuilder query = new BooleanBuilder();
		//
		if(branchCompanyStaff.getDirectorFlag()){
			if(state == 1){
				query.and(QBranchCompanyWithdrawOrder.state.eq(State_BranchCompanyWithdrawOrder.Verifing));
			}else if (state == 2){
				query.and(QBranchCompanyWithdrawOrder.state.eq(State_BranchCompanyWithdrawOrder.Finished));
			}
			Page<TB_BranchCompanyWithdrawOrder> page = service_BranchCompanyWithdrawOrder.page(
					it->it.branchCompanyStaff.eq(branchCompanyStaff).and(query), 
					PageRequest.of(startPage, 5),
					it->new QSort(it.submitDate.desc())
				);
			return page.map(it->new Output(it));
		}else{
			if(state == 1){
				query.and(QBranchCompanyStaffWithdrawOrder.state.eq(State_BranchCompanyStaffWithdrawOrder.Verifing));
			}else if (state == 2){
				query.and(QBranchCompanyStaffWithdrawOrder.state.eq(State_BranchCompanyStaffWithdrawOrder.Finished));
			}
			Page<TB_BranchCompanyStaffWithdrawOrder> page = service_BranchCompanyStaffWithdrawOrder.page(
					it->it.branchCompanyStaff.eq(branchCompanyStaff).and(query), 
					PageRequest.of(startPage, 5),
					it->new QSort(it.submitDate.desc())
				);
			return page.map(it->new Output(it));
		}
	}
	
	@AllArgsConstructor
	private static class Output { 
		@Getter private Long id;
		@Getter private String number;
		@Getter private Double money;
		@Getter private String state;
		@Getter private String stateComment;
		@Getter private String submitDate;
		@Getter private String verifyDate;
		@Getter private String realName;
		@Getter private String accountNumber;
		@Getter private String bank;
		@Getter private String verifyComment;
		
		Output(TB_BranchCompanyWithdrawOrder order){
			this.id = order.getId();
			this.number = order.getNumber();
			this.state = order.getState().name();
			this.stateComment = order.getState().getComment();
			this.money = order.getMoney();
			this.submitDate = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss").format(order.getSubmitDate());
			this.verifyDate = null == order.getVerifyDate() ? "" : new SimpleDateFormat("yyyy/MM/dd hh:mm:ss").format(order.getVerifyDate());
			this.realName = null == order.getRealName() ? "" : order.getRealName();
			this.accountNumber = null == order.getAccountNumber() ? "" : order.getAccountNumber();
			this.bank = null == order.getBank() ? "" : order.getBank();
			this.verifyComment = null == order.getVerifyComment() ? "" : order.getVerifyComment();
		}
		
		Output(TB_BranchCompanyStaffWithdrawOrder order){
			this.id = order.getId();
			this.number = order.getNumber();
			this.state = order.getState().name();
			this.stateComment = order.getState().getComment();
			this.money = order.getMoney();
			this.submitDate = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss").format(order.getSubmitDate());
			this.verifyDate = null == order.getVerifyDate() ? "" : new SimpleDateFormat("yyyy/MM/dd hh:mm:ss").format(order.getVerifyDate());
			this.realName = null == order.getRealName() ? "" : order.getRealName();
			this.accountNumber = null == order.getAccountNumber() ? "" : order.getAccountNumber();
			this.bank = null == order.getBank() ? "" : order.getBank();
			this.verifyComment = null == order.getVerifyComment() ? "" : order.getVerifyComment();
		}
	}

}
